/*
Copyright 2008  Four Roads LLC conact@4-roads.com

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either 
version 2.1 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public 
License along with this library.  If not, see <http://www.gnu.org/licenses/>.
*/
using System;
using System.Collections;
using System.Web.UI.WebControls;
using CommunityServer.Components;
using CommunityServer.Controls;
using System.Reflection;

namespace FourRoads.Links.Controls
{
	/// <summary>
	/// Summary description for ResLinkAdminMoveLink.
	/// </summary>
	public class ResLinkMoveLink: ResLinkAdminTemplatedWebControl
	{
		public ResLinkMoveLink()
		{
			
		}

		#region Child Controls
			Button _Cancel = null;
			Button _Move = null;
			ResLinkLinkView _LinkView = null;
			ResLinkListBox _LinkDestination = null;
		#endregion

		protected override void AttachChildControls()
		{
			_Move = FindControl("MoveLink") as Button;
			_Cancel = FindControl("CancelMove") as Button;
			_LinkView = FindControl("LinkToMove") as ResLinkLinkView;
			_LinkDestination =  FindControl("LinksListBox") as ResLinkListBox;

			_Cancel.Click += new EventHandler(_Cancel_Click);
			_Move.Click += new EventHandler(_Move_Click);
		}

		private ResLinkLink Link
		{
			get
			{
				int postID = CSContext.Current.GetIntFromQueryString("postID" , -1);
				if (postID != -1)
				{
					CSContext.Current.PostID = postID;
					return ResLinkLink.GetSingleLink(postID);
				}

				return null;
			}
		}

		public override void DataBind()
		{
			base.DataBind();

			_LinkView.LinkPost = Link;
			_LinkView.DataBind();
		}

		private void RedirectToOrginalPage()
		{
			string returnURL = Page.Request.QueryString["returnURL"];

			if (returnURL == null)
			{
				Page.Response.Redirect(SiteUrls.Instance().Home);
			}
			else
			{
				Page.Response.Redirect(Page.Server.UrlDecode(returnURL));
			}
		}

		protected override void OnLoad(EventArgs e)
		{
			CSContext.Current.PostID = CSContext.Current.GetIntFromQueryString("PostID" , -1);

			if (CSContext.Current.PostID == -1)
				throw new Exception("Missing query string parameter for move screen");

			base.OnLoad (e);

			//Must be able to remove the link from the section
			ResLinkPermission.AccessCheck(ThisSection, ResLinkPermissionEnum.RemoveLinks , CSContext.Current.User );

			if(!Page.IsPostBack)
			{
				DataBind();
			}
		}

		private void _Cancel_Click(object sender, EventArgs e)
		{
			 RedirectToOrginalPage();
		}

		private void _Move_Click(object sender, EventArgs e)
		{
			int destSectId = System.Convert.ToInt32(_LinkDestination.Tree.SelectedNode.ID);
			
			if (destSectId > -1)
			{
				ResLinkSection.MoveLink(Link , destSectId);

				RedirectToOrginalPage();
			}
		}
	}
}
